  //
//  NQRecordManage.m
//  ZLFastDoctor
//
//  Created by 李岚清 on 16/5/24.
//  Copyright © 2016年 LS. All rights reserved.
//

#import "NQRecordManageVc.h"
#import "NQAddRecordBtn.h"
#import "NQAddRecordVc.h"
#import "NQRecordManageCell.h"
#import "NQRecord.h"
#import "NQRecordManageVc.h"
#import "OKSickCaseController.h"
#import "NQSaveTool.h"
static NSString *NQNQRecordManageIdentifier = @"NQNQRecordManageIdentifier";

#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import <Masonry/Masonry.h>

@interface NQRecordManageVc ()<UITableViewDataSource,UITableViewDelegate,NQAddRecordVcDelegate>
//******************头部View的子控件
/* 头像 */
@property (nonatomic, strong) UIImageView *iconView;
/* 名字View */
@property (nonatomic, strong) UIImageView *nameView;
/* idView */
@property (nonatomic, strong) UIImageView *idView;
/* 手机View */
@property (nonatomic, strong) UIImageView *iphoneView;
/* 名字 */
@property (nonatomic, strong) UILabel *nameField;
/* id号 */
@property (nonatomic, strong) UILabel *idField;
/* 手机号 */
@property (nonatomic, strong) UILabel *iphoneField;
/* 分割线 */
@property (nonatomic, strong) UIView *selectedView;
/* 分割线 */
@property (nonatomic, strong) UIView *selectedIconView;
/* 分割线 */
@property (nonatomic, strong) UIView *selectedNameView;
/* 分割线 */
@property (nonatomic, strong) UIView *selectedIdView;

/**
 *  顶部用户信息视图
 */
@property (nonatomic, strong) UIView *infoView;
/**
 *  底部添加病例按钮
 */
@property (nonatomic, strong) NQAddRecordBtn *addRecordBtn;

/**
 *  中部病例视图
 */
@property (nonatomic, strong) UITableView *recordtableView;
/**
 *  病例数据源数组
 */
@property (nonatomic, strong) NSMutableArray *recordArr;

@end

@implementation NQRecordManageVc
#pragma mark - set/get
- (void)setRecordData:(NSArray *)recordData{
    _recordData = recordData;
    //添加到病例数据源数组中
    [self.recordArr addObjectsFromArray:recordData];
    //刷新数据
    [self.recordtableView reloadData];
}

- (void)setUserInfo:(NQUserInfo *)userInfo{
    _userInfo = userInfo;
    //把传入的用户基本信息赋值上去
    self.iconView.image = userInfo.iconImage;
    self.nameField.text = userInfo.name;
    self.idField.text = userInfo.ID;
    self.iphoneField.text = userInfo.iphone;
}

- (NSMutableArray *)recordArr{
    if (!_recordArr) {
        _recordArr = [NSMutableArray array];
    }
    return _recordArr;
}

#pragma mark - UITableView数据源方法
//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

//每组行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.recordArr.count;
}

//单元格内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //1.创建单元格
    NQRecordManageCell *cell = [tableView dequeueReusableCellWithIdentifier:NQNQRecordManageIdentifier forIndexPath:indexPath];
    
    //2.赋值模型数据
    cell.record = self.recordArr[indexPath.row];
    
    //3.返回单元格
    return cell;
}

#pragma mark - UITableView代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NQAddRecordVc *addRecordVc = [[NQAddRecordVc alloc] init];
    addRecordVc.delegate = self;
//    addRecordVc.isFirst = YES;
    //传递模型
    addRecordVc.recordSelectedModel = self.recordArr[indexPath.row];
    [self.navigationController pushViewController:addRecordVc animated:YES];
    
}

#pragma mark - NQAddRecordVcDelegate
- (void)didOperation:(NQAddRecordVcType)type oldRecord:(NQRecord *)oldRecord newRecord:(NQRecord *)newRecord{
    if (type == NQAddRecordVcTypeDelete) {
        //删除该模型
        [self.recordArr removeObject:oldRecord];
        //刷新界面
        [self.recordtableView reloadData];
    }else{
        //取出旧模型所在数据源数组的索引
        NSInteger index = [self.recordArr indexOfObject:oldRecord];
        //把新模型跟旧模型替换
        [self.recordArr replaceObjectAtIndex:index withObject:newRecord];
        //刷新界面
        [self.recordtableView reloadData];
    }
    //归档病例数据源数组
    [NQRecord saveRecordArr:self.recordArr.copy];
}


#pragma mark - View生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //重写返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_button_back_01"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    //设置导航栏标题
    self.title = @"病历管理";
    
    //设置背景色
    self.view.backgroundColor = NQColor(230, 230, 238);
    
    //添加顶部用户信息视图
    [self.view addSubview:[self setHeaderView]];
    
    //添加中部病例视图
    [self.view addSubview:self.recordtableView];
    
    //底部添加病例按钮
    [self.view addSubview:self.addRecordBtn];
}

//重写返回按钮,判断是否打开菜单侧滑
- (void)back{
    if ([self.superVc isKindOfClass:[OKSickCaseController class]]) {
        
    }else{
        [NQDrawer open];
    }
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - 点击事件(回调方法)
- (void)addRecord{
    NQAddRecordVc *addRecordVc = [[NQAddRecordVc alloc] init];
    [self.navigationController pushViewController:addRecordVc animated:YES];
    //确认回调
    [addRecordVc setAddRecrdModel:^(NQRecord *recordModel) {
        //添加模型到病例数据源数组中
        [self.recordArr addObject:recordModel];
        //刷新界面
        [self.recordtableView reloadData];
        //归档病例数据源数组
        [NQRecord saveRecordArr:self.recordArr.copy];
    }];
}

#pragma mark - 懒加载子控件
- (UIView *)infoView{
    if (!_infoView) {
        _infoView = [[UIView alloc] init];
        _infoView.backgroundColor = NQColor(237, 239, 241);
        _infoView.frame = CGRectMake(0, 0, NQScreenW, NQScreenH*0.22);//140
    }
    return _infoView;
}

- (UITableView *)recordtableView{
    if (!_recordtableView) {
        _recordtableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NQScreenH*0.22, NQScreenW, NQScreenH*0.7) style:UITableViewStylePlain];
        _recordtableView.backgroundColor = NQBgColor;
        _recordtableView.tableFooterView = [[UIView alloc] init];
        //取消cell的选中状态
        _recordtableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        //设置数据源和代理
        _recordtableView.dataSource = self;
        _recordtableView.delegate = self;
        //注册cell
        UINib *nib = [UINib nibWithNibName:@"NQRecordManageCell" bundle:nil];
        [_recordtableView registerNib:nib forCellReuseIdentifier:NQNQRecordManageIdentifier];
    }
    return _recordtableView;
}

- (NQAddRecordBtn *)addRecordBtn{
    if (!_addRecordBtn) {
        _addRecordBtn = [[NQAddRecordBtn alloc] init];
        [_addRecordBtn setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
        [_addRecordBtn setTitleColor: NQColor(50, 190, 190) forState:UIControlStateNormal];
        [_addRecordBtn setTitle:@"添加病例" forState:UIControlStateNormal];
        [_addRecordBtn addTarget:self action:@selector(addRecord) forControlEvents:UIControlEventTouchUpInside];
        _addRecordBtn.frame = CGRectMake(0, NQScreenH - 64 - NQScreenH*0.08, NQScreenW, NQScreenH*0.08);//50
        _addRecordBtn.backgroundColor = [UIColor whiteColor];
    }
    return _addRecordBtn;
}

#pragma mark - 懒加载头部子控件
- (UILabel *)nameField{
    if (!_nameField) {
        _nameField = [[UILabel alloc] init];
        _nameField.text = @"凤凰涅槃";
    }
    return _nameField;
}
- (UILabel *)idField{
    if (!_idField) {
        _idField = [[UILabel alloc] init];
        _idField.text = @"520520520520520";
    }
    return _idField;
}
- (UILabel *)iphoneField{
    if (!_iphoneField) {
        _iphoneField = [[UILabel alloc] init];
        if ([NQSaveTool objectForKey:NQiphone]) {
            _iphoneField.text = [NQSaveTool objectForKey:NQiphone];
        }else{
            _iphoneField.text = @"10086";
        }
    }
    return _iphoneField;
}

- (UIImageView *)iconView{
    if (!_iconView) {
        _iconView = [[UIImageView alloc] init] ;
        _iconView.image = [UIImage imageNamed:@"name"];
        _iconView.layer.cornerRadius = 50;
        _iconView.layer.masksToBounds = YES;
    }
    return _iconView;
}

- (UIImageView *)nameView{
    if (!_nameView) {
        _nameView = [[UIImageView alloc] init];
        _nameView.image = [UIImage imageNamed:@"0"];
    }
    return _nameView;
}

- (UIImageView *)idView{
    if (!_idView) {
        _idView = [[UIImageView alloc] init];
        _idView.image = [UIImage imageNamed:@"idCard"];
    }
    return _idView;
}

- (UIImageView *)iphoneView{
    if (!_iphoneView) {
        _iphoneView = [[UIImageView alloc] init];
        _iphoneView.image = [UIImage imageNamed:@"login_phone"];
    }
    return _iphoneView;
}

- (UIView *)selectedView{
    if (!_selectedView) {
        _selectedView = [[UIView alloc] init];
        _selectedView.backgroundColor = [UIColor lightGrayColor];
    }
    return _selectedView;
}

- (UIView *)selectedIconView{
    if (!_selectedIconView) {
        _selectedIconView = [[UIView alloc] init];
        _selectedIconView.backgroundColor = [UIColor lightGrayColor];
    }
    return _selectedIconView;
}

- (UIView *)selectedNameView{
    if (!_selectedNameView) {
        _selectedNameView = [[UIView alloc] init];
        _selectedNameView.backgroundColor = [UIColor lightGrayColor];
    }
    return _selectedNameView;
}

- (UIView *)selectedIdView{
    if (!_selectedIdView) {
        _selectedIdView = [[UIView alloc] init];
        _selectedIdView.backgroundColor = [UIColor lightGrayColor];
    }
    return _selectedIdView;
}

#pragma mark - 封装头部子控件
- (UIView *)setHeaderView{
    //添加子控件
    [self.infoView addSubview:self.iconView];
    [self.infoView addSubview:self.nameView];
    [self.infoView addSubview:self.idView];
    [self.infoView addSubview:self.iphoneView];
    [self.infoView addSubview:self.nameField];
    [self.infoView addSubview:self.selectedIconView];
    [self.infoView addSubview:self.idField];
    [self.infoView addSubview:self.selectedIdView];
    [self.infoView addSubview:self.iphoneField];
    [self.infoView addSubview:self.selectedView];
    //设置子控件
    [self.iconView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.infoView).offset(25);
        make.left.equalTo(self.infoView).offset(25);
        make.width.height.equalTo(100);
    }];
    [self.nameView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.right).offset(15);
        make.top.equalTo(self.iconView);
        make.width.height.equalTo(20);
    }];
    [self.idView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.right).offset(15);
        make.centerY.equalTo(self.iconView);
        make.width.height.equalTo(20);
    }];
    [self.iphoneView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.right).offset(15);
        make.bottom.equalTo(self.iconView);
        make.width.height.equalTo(20);
    }];
    [self.nameField makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameView.right).offset(30);
        make.centerY.equalTo(self.nameView);
    }];
    [self.selectedIconView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameView);
        make.right.equalTo(self.infoView);
        make.top.equalTo(self.nameField.bottom).offset(10);
        make.height.equalTo(1);
    }];
    [self.idField makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.idView.right).offset(30);
        make.centerY.equalTo(self.idView);
    }];
    [self.selectedIdView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.idView);
        make.right.equalTo(self.infoView);
        make.top.equalTo(self.idField.bottom).offset(10);
        make.height.equalTo(1);
    }];
    [self.iphoneField makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iphoneView.right).offset(30);
        make.centerY.equalTo(self.iphoneView);
    }];
    [self.selectedView makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.infoView);
        make.height.equalTo(0.8);
    }];
    return self.infoView;
}



@end
